﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using FingerPrint.WebUI.ViewModels;
using FingerPrint.WebUI.DAL;
using FingerPrint.WebUI.Models;

namespace FingerPrint.WebUI.Controllers
{
    public class ProductController : Controller
    {
        private FingerPrintContext db = new FingerPrintContext();

        private UnitOfWork unitOfWork = new UnitOfWork();

        //
        // GET: /Product/

        public ActionResult Index(int id = 2)
        {
            SalesOrder salesOrder = unitOfWork.SalesOrderRepository.GetByID(id);

            // The following is an alternative of unitOfWork replcing "db."
            //SalesOrder salesOrder = db.SalesOrders
            //    //.Include(i => i.Courses)
            //    .Where(i => i.SalesOrderID == id)
            //    .Single();

            PopulateSelectedProductData(salesOrder);

            return View(salesOrder);
        }

        private void PopulateSelectedProductData(SalesOrder salesOrder)
        {
            var allProducts = db.Products;
            var salesOrderProducts = new HashSet<int>(salesOrder.Products.Select(p => p.ProductID));
            var viewModel = new List<SelectedSalesOrderProducts>();
            foreach (var product in allProducts)
            {
                viewModel.Add(new SelectedSalesOrderProducts
                {
                    ProductID = product.ProductID,
                    Description = product.Description,
                    ProductCategoryID = product.ProductCategoryID,
                    ProductGroupID = product.ProductGroupID,

                    Selected = salesOrderProducts.Contains(product.ProductID)
                });
            }
            ViewBag.Products = viewModel;
        }

        //
        // GET: /Product/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Product/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Product/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Product/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Product/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Product/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Product/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
